package Arithmetic;

import java.util.Arrays;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-11-18
 * Time: 16:53
 */
public class Simulation {
    //495.提莫攻击
    public static int findPoisonedDuration(int[] timeSeries, int duration) {
        if(timeSeries.length == 1) return duration;//处理边界情况
        int sumTime = 0;
        int i = 0;
        for(;i < timeSeries.length - 1;i++){
            if(timeSeries[i] + duration <= timeSeries[i + 1])
                sumTime += duration;
            else//中毒时间内又进行攻击
                sumTime += timeSeries[i + 1] - timeSeries[i];
        }
        return sumTime + duration;
    }

    //1576.替换所有的问号
    public static String modifyString(String ss) {
        char[] s = ss.toCharArray();
        for(int i = 0;i < s.length;i++){
            if(s[i] == '?'){
                for(char j = 'a';j <= 'z';j++){
                    if((i == 0 || j != s[i - 1]) && (i == s.length - 1 || j != s[i + 1])){
                        s[i] = j;
                        break;
                    }
                }
            }
        }
        return new String(s);
    }

    //6.Z字形变换
    public static String convert(String s, int numRows) {
        if(numRows == 1) return s;
        int len = s.length();
        //计算公差
        int d = 2 * numRows - 2;

        StringBuilder stringBuilder = new StringBuilder();

        //处理第一行
        for(int i = 0;i < len;i+=d){
            stringBuilder.append(s.charAt(i));
        }

        //处理中间行
        for(int i = 1;i < numRows - 1;i++){
            // for(int j = i,k = d - i;j < len || k < len;j += d,k += d){
            //     if(j < len) stringBuilder.append(s.charAt(j));
            //     if(k < len) stringBuilder.append(s.charAt(k));
            // }

            //?为什么这种写法是错的
            for(int j = i;j < len;j += d){
                stringBuilder.append(s.charAt(j));
            }

            for(int k = d - i;k < len;k += d){
                stringBuilder.append(s.charAt(k));
            }
        }

        //处理最后一行
        for(int i = numRows - 1;i < len;i += d){
            stringBuilder.append(s.charAt(i));
        }

        return stringBuilder.toString();
    }

    //38.外观数列
    public static String countAndSay(int n) {
        if(n == 1) return "1";
        StringBuilder str = new StringBuilder("1");
        for(int i = 2;i <= n;i++){
            StringBuilder newStr = new StringBuilder();
            int cur = 0;
            int prev = 0;
            for(;cur < str.length();){
                int count = 0;
                while(cur < str.length() && str.charAt(cur) == str.charAt(prev)){
                    count++;
                    cur++;
                }
                newStr.append("" + count + str.charAt(prev));
                prev = cur;
            }
            str = newStr;
        }

        return str.toString();
    }
    //1419.数青蛙
    public static int minNumberOfFrogs(String ss) {
        char[] c = {'c','r','o','a','k'};
        char[] s = ss.toCharArray();

        //创建哈希表存储蛙叫字符个数
        HashMap<Character,Integer> map = new HashMap<>();
        //初始化
        for (int i = 0; i < c.length; i++) {
            map.put(c[i], 0);
        }

        for(int i = 0;i < s.length;i++){
            if(s[i] == 'c'){
                //减少青蛙数量
                if(map.get('k') == 0){
                    map.put('c',map.get('c') + 1);
                }else{
                    map.put('k',map.get('k') - 1);
                    map.put('c',map.get('c') + 1);
                }
            }else{
                int index = find(c,s[i]);
                if(map.get(c[index - 1]) == 0){
                    return -1;
                }else{
                    map.put(c[index - 1],map.get(c[index - 1]) - 1);
                    map.put(s[i],map.get(s[i]) + 1);
                }
            }
        }
        //判断青蛙是否全部走到k下标
        for (char c1 :c){
            if(c1 != 'k' && map.get(c1) != 0){
                return -1;
            }
        }
        return map.get('k');
    }

    public static int find(char[] s,char c){
        for(int i = 1;i < s.length;i++){
            if(s[i] == c)
                return i;
        }
        return -1;
    }

    public static void main(String[] args) {
        //495.提莫攻击
        int time = findPoisonedDuration(new int[]{1,2,3,4},2);
        System.out.println(time);

        //1576.替换所有的问号
        String newStr = modifyString("?adr?");
        System.out.println(newStr);

        //6.Z字形变换
        String str = convert("abcde",3);
        System.out.println(str);

        //38.外观数列
        str = countAndSay(4);
        System.out.println(str);

        //1419.数青蛙
        int count = minNumberOfFrogs("crcoakroak");
        System.out.println(count);
    }
}
